﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel.DataAnnotations;
using System.Reflection;
using System.ServiceModel.DomainServices.Client;
using System.Windows;
using System.Windows.Controls;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using InsuranceAdjuster;
using InsuranceAdjuster.Helpers;
using InsuranceAdjuster.OccasionallyConnected;
using InsuranceAdjuster.OccasionallyConnected.Messages;
using InsuranceAdjuster.Services;
using InsuranceAdjuster.Web.Models;
using Papa.Common;
using System.ComponentModel;


namespace InsuranceAdjuster.ViewModels
{
    public class ClientAnchorsViewModel : ViewModelBase
    {
        private ObservableCollection<String> _ClientAnchors = new ObservableCollection<String>();

        public ObservableCollection<String> ClientAnchors
        {
            get { return _ClientAnchors; }
        }

        public ClientAnchorsViewModel(IPageConductor pageConductor)
        {
            this.PageConductor = PageConductor;
            RegisterCommands();
            EntityManager.GetObjectStore().PropertyChanged += new PropertyChangedEventHandler(ObjectStore_PropertyChanged);
            RefreshAnchorList();
        }

        void ObjectStore_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            switch (e.PropertyName)
            {
                case "ClientAnchors":
                    RefreshAnchorList();
                    break;
            }
        }

        private void RefreshAnchorList()
        {
            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                _ClientAnchors.Clear();
                foreach (var Item in EntityManager.GetObjectStore().ClientAnchors.OrderBy(x => x.Key))
                {
                    _ClientAnchors.Add(String.Format("{0} : {1}", Item.Key, Item.Value));
                }
                RaisePropertyChanged("ClientAnchors");
            });
        }
    }
}
